package com.dd.stack.redis.config.redisConfig.msgListener;

import com.dd.stack.redis.pojo.BO.OrderBO;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/1/31 11:58
 * @Description OrderMessageListener
 */
@Slf4j
@Component
public class OrderMessageListener implements MessageListener {

    @Override
    @Transactional
    public void onMessage(Message message, byte[] pattern) {
        // 获取订单信息
        String orderJson = new String(message.getBody());
        log.info("Received order message: " + orderJson);
        // 解析订单信息
        OrderBO orderBO = new Gson().fromJson(orderJson, OrderBO.class);
        // 处理订单逻辑
        processOrder(orderBO);
    }

    private void processOrder(OrderBO orderBO) {
        log.info("Processing order: " + orderBO.getOrderId());
        // 模拟订单处理逻辑
        // (1) 更新库存
        // (2) 生成订单DO并持久化
        // (3) 邮件或短信通知
        log.info("Order processed successfully for user: " + orderBO.getUserId());
        // 可以在这里调用实际的订单处理服务，例如保存订单到数据库、发送邮件通知等
    }
}
